﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewAllScholarshipsReceivedByStudent : System.Web.UI.Page
    {
        string queryString;
        StudentAL studentAL;
        FundAL fundAL;
        Guid studentId;
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 5;
        AdminAL adminAL;
        MemberAL memberAL;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                studentId = Guid.Empty;
                bool convertableToBool = Guid.TryParse(queryString, out studentId);
                if (convertableToBool)
                {
                    BindData();
                }
                else
                {
                    return;
                }

            }
        }


        private void BindData()
        {

            studentAL = new StudentAL();
            fundAL = new FundAL();
            memberAL = new MemberAL();
            List<ScholarshipDataDTO> allScholarshipDataOfStudent = fundAL.GetAllScholarShipDataOfStudent(studentId).ToList();
            List<ScholarshipDataDTO> allScholarshipDataOfStudentOfOnePage = new List<ScholarshipDataDTO>();
            //gvViewAllMembers.DataSource = allMembers;
            //gvViewAllMembers.DataBind();


            // get the total rows 
            double totalRows = (int)allScholarshipDataOfStudent.Count;

            lblAllScholarshipOfStudentTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllScholarshipOfStudentCurrPage.Text = currentPageNumber.ToString();

            lblAllScholarshipOfStudentPage.Text = "Displaying all of the scholarships received by a student  - Page ";
            lblAllScholarshipOfStudentOf.Text = "  of  ";

            int totalPageNumber = lblAllScholarshipOfStudentTotalPage.Text == "" ? 0 : Convert.ToInt32(lblAllScholarshipOfStudentTotalPage.Text);

            if (totalPageNumber == 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllScholarshipOfStudentFirst.Enabled = false;
                    btnAllScholarshipOfStudentPrev.Enabled = false;
                    btnAllScholarshipOfStudentNext.Enabled = false;
                    btnAllScholarshipOfStudentLast.Enabled = false;
                    allScholarshipDataOfStudentOfOnePage = allScholarshipDataOfStudent;
                }

            }
            else if (totalPageNumber > 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllScholarshipOfStudentFirst.Enabled = false;
                    btnAllScholarshipOfStudentPrev.Enabled = false;
                    btnAllScholarshipOfStudentNext.Enabled = true;
                    btnAllScholarshipOfStudentLast.Enabled = true;
                    allScholarshipDataOfStudentOfOnePage = allScholarshipDataOfStudent.GetRange(0, PAGE_SIZE);
                }
                else if (currentPageNumber < totalPageNumber && currentPageNumber > 1)
                {
                    btnAllScholarshipOfStudentFirst.Enabled = true;
                    btnAllScholarshipOfStudentPrev.Enabled = true;
                    btnAllScholarshipOfStudentNext.Enabled = true;
                    btnAllScholarshipOfStudentLast.Enabled = true;
                    allScholarshipDataOfStudentOfOnePage = allScholarshipDataOfStudent.GetRange(((currentPageNumber - 1) * PAGE_SIZE), PAGE_SIZE);

                }
                else if (currentPageNumber == totalPageNumber)
                {
                    btnAllScholarshipOfStudentFirst.Enabled = true;
                    btnAllScholarshipOfStudentPrev.Enabled = true;
                    btnAllScholarshipOfStudentNext.Enabled = false;
                    btnAllScholarshipOfStudentLast.Enabled = false;
                    allScholarshipDataOfStudentOfOnePage = allScholarshipDataOfStudent.GetRange(((currentPageNumber - 1) * PAGE_SIZE), (allScholarshipDataOfStudent.Count - (currentPageNumber - 1) * PAGE_SIZE));
                }
            }
            

            CreateDynamicTable(allScholarshipDataOfStudentOfOnePage);

        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / PAGE_SIZE);

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllScholarshipOfStudentCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllScholarshipOfStudentCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllScholarshipOfStudentTotalPage.Text);
                    break;
            }


            BindData();
        }


        private void CreateDynamicTable(List<ScholarshipDataDTO> allScholarshipDataOfStudentOfOnePage)
        {
            PlaceHolder4.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allScholarshipDataOfStudentOfOnePage.Count;

            List<StudentDTO> studentDTO = new List<StudentDTO>();
            List<ScholarshipDTO> scholarshipDTO = new List<ScholarshipDTO>();
            List<ScholarshipDataDTO> scholarshipDataDTO = new List<ScholarshipDataDTO>();
            List<MemberDTO> memberDTO = new List<MemberDTO>();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder4.Controls.Add(tbl);

            TableRow trHeader = new TableRow();
            trHeader.Width = new Unit("25%");

            HyperLink hlScholarshipCellHeaderScholarName = new HyperLink();
            hlScholarshipCellHeaderScholarName.Text = "Scholarship Name";

            HyperLink hlScholCellHeaderAmt = new HyperLink();
            hlScholCellHeaderAmt.Text = "Amount";

            HyperLink hlScholCellHeaderAmtSettldAt = new HyperLink();
            hlScholCellHeaderAmtSettldAt.Text = "Amount Settled On";


            HyperLink hlScholCellHeaderAmtSettldBy = new HyperLink();
            hlScholCellHeaderAmtSettldBy.Text = "Amount Settled By";

            HyperLink hlScholCellHeaderAkngdOn = new HyperLink();
            hlScholCellHeaderAkngdOn.Text = "Acknowledged On";

            HyperLink hlScholCellHeaderNotes = new HyperLink();
            hlScholCellHeaderNotes.Text = "Notes";

           

            List<HyperLink> allHeaderHyperlinks = new List<HyperLink>();

            //based  on selected type change column orders


            allHeaderHyperlinks.Add(hlScholarshipCellHeaderScholarName);
            allHeaderHyperlinks.Add(hlScholCellHeaderAmt);
            allHeaderHyperlinks.Add(hlScholCellHeaderAmtSettldAt);
            allHeaderHyperlinks.Add(hlScholCellHeaderAmtSettldBy);
            allHeaderHyperlinks.Add(hlScholCellHeaderAkngdOn);
            allHeaderHyperlinks.Add(hlScholCellHeaderNotes);


            foreach (HyperLink hyperlinkHeader in allHeaderHyperlinks)
            {
                TableCell tcHeader = new TableCell();
                tcHeader.BackColor = System.Drawing.Color.DarkSlateBlue;
                tcHeader.ForeColor = System.Drawing.Color.White;

                tcHeader.Controls.Add(hyperlinkHeader);
                trHeader.Cells.Add(tcHeader);



            }

            tbl.Rows.Add(trHeader);


            // Now iterate through the table and add your controls 
            for (int i = 0; i < tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");

                scholarshipDTO = fundAL.GetScholarShipById(allScholarshipDataOfStudentOfOnePage[i].ScholarshipId).ToList();
                studentDTO = studentAL.GetStudentById(allScholarshipDataOfStudentOfOnePage[i].StudentId).ToList();

                HyperLink hlScholCellScholName = new HyperLink();
                hlScholCellScholName.Text = fundAL.GetScholarShipById(allScholarshipDataOfStudentOfOnePage[i].ScholarshipId)[0].Title;
                hlScholCellScholName.NavigateUrl = "viewScholarshipById.aspx?" + allScholarshipDataOfStudentOfOnePage[i].ScholarshipId ;

                HyperLink hlScholCellAmt = new HyperLink();
                hlScholCellAmt.Text = allScholarshipDataOfStudentOfOnePage[i].amount.ToString();

                HyperLink hlScholCellAmtSettldAt = new HyperLink();
                hlScholCellAmtSettldAt.Text = allScholarshipDataOfStudentOfOnePage[i].amountSettledAt.Value.ToShortDateString();

                HyperLink hlScholCellAmtSettldBy = new HyperLink();
                hlScholCellAmtSettldBy.Text = memberAL.GetMemberById(new Guid(allScholarshipDataOfStudentOfOnePage[i].SettledBy.Value.ToString()))[0].FullName;
                hlScholCellAmtSettldBy.NavigateUrl = "viewMember.aspx?" + allScholarshipDataOfStudentOfOnePage[i].SettledBy;

                HyperLink hlScholCellAkngOn = new HyperLink();
                hlScholCellAkngOn.Text = allScholarshipDataOfStudentOfOnePage[i].AcknowledgeAt.Value.ToShortDateString();

                HyperLink hlScholCellNotes = new HyperLink();
                hlScholCellNotes.Text = allScholarshipDataOfStudentOfOnePage[i].Notes; 

                queryString = Request.QueryString.ToString();



                hlScholarshipOfStudent.NavigateUrl = "viewStudentById.aspx?" + studentDTO[0].StudentId;
                hlScholarshipOfStudent.Text = studentDTO[0].FullName;

                lblScholarshipOfStudent1.Text = "All of the scholarships received by  ";
                lblScholarshipOfStudent.Text = " ";

                //if (studentDTO.Count != 0)
                //{
                //    hlParticpatnOfStudent.Text = studentDTO[0].FullName;
                //    hlParticpatnOfStudent.NavigateUrl = "viewStudentById.aspx?" + studentDTO[0].StudentId;
                //}
                //if (staffDTO.Count != 0)
                //{
                //    hlParticpatnOfStudent.Text = staffDTO[0].FullName;
                //    //this should be redirected to staff page
                //   // hlParticpatnOfStudent.NavigateUrl = "viewStudentById.aspx?" + staffDTO[0].StaffId;
                //}



                List<HyperLink> allHyperlinks = new List<HyperLink>();
                allHyperlinks.Add(hlScholCellScholName);
                allHyperlinks.Add(hlScholCellAmt);
                allHyperlinks.Add(hlScholCellAmtSettldAt);
                allHyperlinks.Add(hlScholCellAmtSettldBy);
                allHyperlinks.Add(hlScholCellAkngOn);
                allHyperlinks.Add(hlScholCellNotes);



                foreach (HyperLink hyperlink in allHyperlinks)
                {
                    TableCell tc = new TableCell();



                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;

                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hyperlink);


                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table

                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
    }
}